import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:old_shop/common/const/cosnt.dart';
import 'package:old_shop/login&regist/view/button_view.dart';
import 'package:old_shop/login&regist/view/normal_text.dart';
import 'package:old_shop/login&regist/view/sms_view.dart';
import 'package:shared_preferences/shared_preferences.dart';

class RegistPage extends StatefulWidget {
  @override
  _LoginPageState createState() => _LoginPageState();
}

class _LoginPageState extends State<RegistPage> {
  FocusNode _focusNode = FocusNode();
  TextEditingController _controller = TextEditingController();
  FocusNode _codefocusNode = FocusNode();
  TextEditingController _codeController = TextEditingController();
  FocusNode _pwdfocusNode = FocusNode();
  TextEditingController _pwdController = TextEditingController();
  FocusNode _inviCodeNode = FocusNode();
  TextEditingController _inviController = TextEditingController();
  String phone = "";
  @override
  void dispose() {
    _controller.dispose();
    _codeController.dispose();
    _pwdController.dispose();
    _focusNode.dispose();
    _codefocusNode.dispose();
    _pwdfocusNode.dispose();
    _inviController.dispose();
    _inviCodeNode.dispose();
    super.dispose();
  }

  void _regist() {
    if (_controller.text.length < 11) {
      EasyLoading.showError("请输入11位手机号码");
      return;
    }
    if (_inviController.text.isEmpty) {
      EasyLoading.showError("请输入邀请码");
      return;
    }

    if (_codeController.text.length < 6) {
      EasyLoading.showError("请输入6位验证码");
      return;
    }

    _login(context);
  }

  void _jumpPwdlogin() {
    Navigator.pushNamed(context, "/pwdlogin", arguments: {"phone": _controller.text});
  }

  void _jumpToRoot(BuildContext context) {
    SharedPreferences.getInstance().then((value) => value.setBool(AppConst.APP_IS_LOGIN, true));
    Navigator.of(context).pushNamedAndRemoveUntil('/root', (Route<dynamic> route) => false);
  }

  void _login(BuildContext context) async {
    EasyLoading.show(status: "Loading...");
    await Future.delayed(const Duration(seconds: 1));
    EasyLoading.dismiss();
    _jumpToRoot(context);
  }

  @override
  Widget build(BuildContext context) {
    final size = MediaQuery.of(context).size;

    return Scaffold(
      resizeToAvoidBottomInset: false,
      backgroundColor: Colors.white,
      body: GestureDetector(
        behavior: HitTestBehavior.translucent,
        onTap: () {
          _focusNode.unfocus();
          _codefocusNode.unfocus();
          _inviCodeNode.unfocus();
        },
        child: SafeArea(
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              IconButton(
                icon: Icon(Icons.arrow_back_ios),
                onPressed: () => Navigator.pop(context),
              ),
              Padding(
                padding: const EdgeInsets.only(left: 30, right: 30),
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: <Widget>[
                    SizedBox(
                      height: 40,
                    ),
                    Text(
                      "注册账号",
                      style: AppFont.textStyle(25, color: AppColor.font333, fontWeight: FontWeight.bold),
                    ),
                    SizedBox(
                      height: 60,
                    ),
                    NormalTextfield(
                      hintText: "请输入手机号码",
                      backgroundColor: Colors.white,
                      inputFormatters: [FilteringTextInputFormatter.digitsOnly],
                      keyboardType: TextInputType.number,
                      focusNode: _focusNode,
                      maxLength: 11,
                      controller: _controller,
                      onSubmited: (val) {
                        setState(() {
                          phone = val;
                        });
                      },
                      onChanged: (val) {
                        print(val);
                        setState(() {
                          phone = val;
                        });
                      },
                    ),
                    SizedBox(height: 1, child: Divider()),
                    SizedBox(
                      height: 8,
                    ),
                    Row(
                      children: [
                        Expanded(
                          child: NormalTextfield(
                            hintText: "请输入验证码",
                            backgroundColor: Colors.white,
                            maxLength: 6,
                            inputFormatters: [FilteringTextInputFormatter.digitsOnly],
                            keyboardType: TextInputType.number,
                            focusNode: _codefocusNode,
                            controller: _codeController,
                          ),
                        ),
                        SmsCounterView(
                          phone: phone,
                        )
                      ],
                    ),
                    SizedBox(height: 1, child: Divider()),
                    SizedBox(
                      height: 8,
                    ),
                    NormalTextfield(
                      hintText: "请输入登录密码",
                      backgroundColor: Colors.white,
                      focusNode: _pwdfocusNode,
                      obscureText: true,
                      maxLength: 20,
                      controller: _pwdController,
                    ),
                    SizedBox(height: 1, child: Divider()),
                    NormalTextfield(
                      hintText: "请输入邀请码",
                      backgroundColor: Colors.white,
                      focusNode: _inviCodeNode,
                      controller: _inviController,
                    ),
                    SizedBox(height: 1, child: Divider()),
                  ],
                ),
              ),
              BottomButton(
                margin: const EdgeInsets.only(top: 75, left: 15, right: 15),
                title: "注册并登录",
                onTap: _regist,
              ),
            ],
          ),
        ),
      ),
    );
  }
}
